import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCUpdate {
    public static void main(String[] args) throws SQLException {
        //第一步, 数据库位置
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/attendance?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");

        //第二步, 开始连接
        Connection connection = dataSource.getConnection();

        //第三步, 构造sql语句
        Scanner in = new Scanner(System.in);
        System.out.println("请输入你要修改的名字");
        String name = in.next();
        System.out.println("请输入待修改学号");
        int id = in.nextInt();

        String sql = "update student set name = ? where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(2, id);
        statement.setString(1, name);

        //第四步, 发送请求给服务器
        statement.executeUpdate();

        //第五步, 关闭资源
        statement.close();
        connection.close();
    }
}
